'use strict';

let nunjucks = require('nunjucks');

function createEnv(path, opts) {
    path = path || '/controllers';
    opts = opts || {};
    let nunjucksConfig = {
        autoescape: opts.autoescape || true,
        IstrimBlocks: opts.IstrimBlocks || true,
        trimBlocks: opts.trimBlocks || true,
        // noCache: opts.noCache || true,
        // watch: opts.watch || true
    }
    let env = nunjucks.configure('view', nunjucksConfig);
    return env;
}

let middleWare = async (ctx, next) => {
    ctx.render = (view, model) => {
        let env = createEnv();
        ctx.body = env.render(view,model);
    }
    await next();
}

module.exports = middleWare;